﻿ #region Using Directives

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Configuration;

using QuickDev.Common.Audit;
using QuickDev.Common.Utils;
using QuickDev.Localization;

using QuickDev.Web.UI.Controls;
using QuickDev.Web.UI;

using QuickDev.Model;
using QuickDev.Caching;
using QuickDev.BLL;
using QuickDev.Common.Serialization;

#endregion

namespace ReturnDB.Taobao.WebApp.controls
{
    public partial class ucPickLine : AdminControl
    {
        #region Member Variables

        Serializer serializer = new Serializer();

        #endregion

        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load( object sender , EventArgs e )
        {
            try
            {
                if ( !IsPostBack )
                {
                }
            }
            catch ( System.Exception ex )
            {
                Logger.Error( typeof( ucPickLine ).Name + ".Page_Load" , ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// This method clears the list of field values.
        /// </summary>
        public void Clear()
        {
            ddlQty.SelectedIndex = -1;
            ddlReturnTypes.SelectedIndex = -1;
            cblReturnReasons.SelectedIndex = -1;
            txtProductNumber.Text = String.Empty;
            txtOtherReasonContent.Text = String.Empty;
        }

        #endregion

        #region Private

        public void LoadQty()
        {
            SiteSettings siteSettings = null;
            try
            {
                siteSettings = SiteSettingsCache.GetCachedModel();
            }
            catch ( Exception )
            {
                //swallow it - probably no connection string during install
            }

            for ( int i = 1 ; i <= siteSettings.MaximumReturnOrderItems ; i++ )
            {
                ddlQty.Items.Add( new ListItem( i.ToString() , i.ToString() ) );
            }
        }

        public void LoadReturnTypes()
        {
            Utility.LoadListControl( ddlReturnTypes , ReturnTypeCache.GetList( false ) , "Name" , "Id" );
        }

        public void LoadReturnReasons()
        {
            Utility.LoadListControl( cblReturnReasons , ReturnReasonCache.GetList( false ) , "Name" , "Id" );
        }

        #endregion

        #endregion

        #region Properties

        /// <summary>
        /// This property represents the AND, OR, AND NOT, OR NOT values. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        ///  We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The boolean operator.</value>
        public int Qty
        {
            get
            {
                if ( ddlQty.Items.FindByValue( ddlQty.SelectedValue ) == null )
                    throw new Exception( "Invalid Qty" );

                int operatorId = 0;
                int.TryParse( ddlQty.SelectedValue , out operatorId );

                if ( operatorId > 0 )
                {
                    return operatorId;
                }

                throw new Exception( "Invalid Qty" );
            }
            set
            {
                ddlQty.SelectedValue = value.ToString();
            }
        }

        /// <summary>
        /// This property represents the name of the field. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        /// We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The name of the field.</value>
        public string ProductNumber
        {
            get
            {
                return txtProductNumber.Text.Trim();
            }
            set
            {
                txtProductNumber.Text = value;
            }
        }

        /// <summary>
        /// This property represents the name of the field. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        /// We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The name of the field.</value>
        public string OtherReasonContent
        {
            get
            {
                return txtOtherReasonContent.Text.Trim();
            }
            set
            {
                txtOtherReasonContent.Text = value;
            }
        }

        /// <summary>
        /// This property represents the name of the field. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        /// We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The name of the field.</value>
        public bool OtherReasonChecked
        {
            get
            {
                return cbOtherReasonChecked.Checked;
            }
            set
            {
                cbOtherReasonChecked.Checked = value;
            }
        }

        /// <summary>
        /// This property represents the type of comparison. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        /// We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The comparison operator.</value>
        public int ReturnTypeId
        {
            get
            {
                if ( ddlReturnTypes.Items.FindByValue( ddlReturnTypes.SelectedValue ) == null )
                    throw new Exception( "Invalid Return Type" );

                int operatorId = 0;
                int.TryParse( ddlReturnTypes.SelectedValue , out operatorId );
                if ( operatorId > 0 )
                {
                    return operatorId;
                }

                throw new Exception( "Invalid Return Type" );
            }
            set
            {
                ddlReturnTypes.SelectedValue = value.ToString();
            }
        }

        /// <summary>
        /// This property represents the type of comparison. Notice
        /// that we check whether the posted value actually exists in the drop down list.
        /// We do this to prevent SQL Injection Attacks.
        /// </summary>
        /// <value>The comparison operator.</value>
        public string ReturnReasonXML
        {
            get
            {
                List<ReturnReason> list = new List<ReturnReason>();
                foreach ( ListItem item in cblReturnReasons.Items )
                {
                    if ( item.Selected )
                    {
                        list.Add( ReturnReasonCache.GetModel( int.Parse( item.Value ) ) );
                    }
                }

                return serializer.SerializeObject( list , typeof( List<ReturnReason> ) );
            }
            set
            {
                List<ReturnReason> list = serializer.DeserializeObject( value , typeof( List<ReturnReason> ).AssemblyQualifiedName ) as List<ReturnReason>;
                foreach ( ReturnReason model in list )
                {
                    foreach ( ListItem item in cblReturnReasons.Items )
                    {
                        if ( item.Value == model.Id.ToString() && item.Text == model.Name )
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// This property contains represents the SQL clause used when building the query.
        /// </summary>
        /// <value>The query clause.</value>
        public ReturnOrderDetail ROD
        {
            get
            {
                if ( String.IsNullOrEmpty( this.ProductNumber ) )
                    return null;

                ReturnOrderDetail model = new ReturnOrderDetail();
                model.ProductNumber = ProductNumber;
                model.ProductQty = Qty;
                model.ReturnTypeId = ReturnTypeId;
                model.ReturnReasonXML = ReturnReasonXML;
                model.OtherReasonContent = OtherReasonContent;
                model.OtherReasonChecked = OtherReasonChecked;
                return model;
            }
        }

        #endregion
    }
}
